/***************************************
 2010-4-30
****************************************/
package com.happy.common.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
	public static final int OXFF = 0xff;
	public static final int OX10 = 0x10;

	/**默认无参构造函数**/
	public MD5() {
	}
	public static String crypt(String str) throws NoSuchAlgorithmException {
		if (str == null || str.length() == 0) {
		throw new IllegalArgumentException("String to encript cannot be null or zero length");
		}
		StringBuffer hexString = new StringBuffer();


		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(str.getBytes());
		byte[] hash = md.digest();


		for (byte aHash : hash) {
		if ((OXFF & aHash) < OX10) {
		hexString.append("0").append(Integer.toHexString((OXFF & aHash)));
		} else {
		hexString.append(Integer.toHexString(OXFF & aHash));
		}
		}


		return hexString.toString();

	}

}
